class TransactionsController < ApplicationController

  before_filter :admin_check, except: [:show, :index, :new, :create, :all_transactions, :destroy]
  before_filter :owner_check, only: [:edit, :update, :destroy]
  helper_method :sort_column, :sort_direction

  def all_transactions
    status = params[:status]
    @transactions = Transaction.all_transactions(
      current_user, params[:status]
    ).paginate(
      page: params[:page], 
      per_page: 10,
      order: "#{params[:sort]} #{params[:direction]}"
    )

    respond_to do |format|
      format.html
    end
  end
  
  def index
    status = params[:status]
    @transactions = Transaction.by_status(status).by_user(current_user).where(
      book_id: params[:book_id]
    ).paginate(
      page: params[:page],
      per_page: 10,
      order: "#{params[:sort]} #{params[:direction]}"
    )
    
    respond_to do |format|
      format.html
    end
  end

  def new
    @book = Book.find(params[:book_id])
    error = nil

    if @book.nil?
      error = "You haven't choose any book."
    else
      @transaction = Transaction.new
    end

    respond_to do |format|
      if error.present?
        format.html { redirect_to books_path, notice: error}
      else
        format.html
      end
    end
  end

  def create
    @book = Book.find(params[:book_id])
    params[:transaction] ||= {}
    @transaction = Transaction.new(params[:transaction].merge(book: @book, user: current_user))
    @transaction.status = Transaction::STATES[:WAITING]

    respond_to do |format|
      if @transaction.save
        format.html { redirect_to book_transactions_path, notice: "You have borror the book successfully"}
      else
        format.html { render action: "new" }
      end
    end
  end

  def edit
    @transaction = Transaction.find[params[:id]]
    @book = Book.find[params[:book_id]]
  end

  def update
    debugger

    @transaction = Transaction.find(params[:id])
    respond_to do |format|
      if @transaction.update_attributes(params[:transaction])
        format.html { redirect_to @transaction, notice: "Transaction is successfully updated." }
      else
        format.html { render action: "edit" }
      end
    end

  end

  def destroy
    @transaction = Transaction.find(params[:id])
    @transaction.destroy

    respond_to do |format|
      format.html { redirect_to book_transactions_path, notice: "Transaction is successfully deleted." }
    end
  end

  def remind
    @transaction = Transaction.find(params[:id])
    UserMailer.reminder_email(@transaction).deliver

    respond_to do |format|
      format.html { redirect_to :back }
    end
  end

  def update_status
    errors = []

    begin
      @transaction = Transaction.find(params[:id])
      if current_user.admin?
        if @transaction.status == Transaction::STATES[:WAITING] && params[:status].to_i == Transaction::STATES[:APPROVED]
          @transaction.start_date = Time.now.to_date
          @transaction.end_date = @transaction.start_date +  Transaction::MAX_DAYS_FOR_RETURN
        end
        @transaction.status = params[:status]
      end
    rescue => ex
      errors << ex.message
    end

    respond_to do |format|
      if @transaction.present? && @transaction.save
        #format.html { redirect_to book_transactions_path, notice: "Action is success"}
        format.json {
          render json: {transaction: @transaction}, status: 200
        }
      else
        errors << "Transaction can not be save" if @transaction.present?
        #format.html { redirect_to book_transactions_path, notice: "Action is unsuccess"}
        format.json {
          render json: {errors: errors}, status: 400
        }
      end
    end
  end

  protected
  def sort_column
    Transaction.column_names.include?(params[:sort]) ? params[:sort] : "name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end

  def admin_check
    unless current_user.admin?
      redirect_to all_transactions_path, notice: "You don't have permission to complete the action"
    end
  end

  def owner_check
    @transaction = Transaction.find(params[:id])
    unless current_user.admin? || current_user.id == @transaction.user_id
      redirect_to all_transactions_path, notice: "You don't have permission to complete"
    end
  end

end
